const { dbName, http } = require("../modal/db.config");
const SECRET = dbName;
const { getCurrentDateTime } = require("../modal/time.config");
const STATE = {
  PRE_PAY: 0, //待支付
  PAYED: 1, //已支付/审核中
  CHECK_IN: 2, //入住中
  OUT: 3, //已退租
};
const resolveDataFun = (req, list) => {
  return new Promise((resolve, reject) => {
    let p = list.map(async (v) => {
      v.houseInfo = await req.services.HousesService.findById(v.houseId);
      v.userInfo = await req.services.UserService.findById(v.userId);
      return v;
    });
    Promise.all(p).then((res) => {
      resolve(res);
    });
  });
};
exports.create = async (req, res) => {
  const body = req.body;
  const { houseId, term, totalPrice } = body;
  const user = await req.services.UserService.decodeToken();
  const data = {
    houseId,
    term,
    totalPrice,
    state: STATE.PRE_PAY,
    userId: user.userId,
    createTime: getCurrentDateTime(),
    updateTime: getCurrentDateTime(),
  };
  const result = await req.services.OrderService.create(data);
  if (result.affectedRows === 1) {
    res.status(200).json({ data: result.insertId, msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};
exports.pay = async (req, res) => {
  const body = req.body;
  const { id } = body;
  const state = STATE.PAYED;
  const updateTime = getCurrentDateTime();
  const data = {
    state,
    updateTime,
  };
  const newData = await req.services.OrderService.update(data, { id });
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "更新成功", msg: "success", code: 200 });
  } else {
    res
      .status(200)
      .json({ data: "系统错误，暂无此订单号", msg: "fail", code: 500 });
  }
};

exports.update = async (req, res) => {
  const body = req.body;
  const { houseId, term, totalPrice, id, state } = body;
  const updateTime = getCurrentDateTime();
  const data = {
    houseId,
    term,
    totalPrice,
    state,
    updateTime,
  };
  const newData = await req.services.OrderService.update(data, { id });
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "更新成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};

exports.getDetail = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const newData = await req.services.OrderService.findById(id);
  const resolveData = await resolveDataFun(req, [newData]);
  if (newData) {
    res.status(200).json({ data: resolveData[0], msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无数据", msg: "fail", code: 400 });
  }
};
exports.list = async (req, res) => {
  const body = req.body;
  const { houseId, term, page = null, pageSize = null } = body;
  let newData = await req.services.OrderService.getList({
    pageSize: pageSize,
    page: page,
    where: { houseId, term },
  });
  const resolveData = await resolveDataFun(req, newData);
  let count = await req.services.OrderService.getCount({
    where: { houseId, term },
  });

  res.status(200).json({
    data: {
      list: resolveData,
      total: count,
    },
    msg: "success",
    code: 200,
  });
};

exports.del = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const newData = await req.services.OrderService.delete(Number(id));
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "删除成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无数据", msg: "fail", code: 400 });
  }
};
